const mid_util = require('./mid_util')
const list = 'LIST-DATA'
module.exports = {
  updateByID(listid, value, callBack) {
    this.getTotalLists({
      success: res => {
        var res_tmp = res
        res_tmp.forEach((ele, idx) => {
          if (ele.list_id == listid) {
            res_tmp[idx] = value
          }
        })
        mid_util.setStorage(list, res_tmp, {
          success: res => {
            mid_util.callBack(callBack, '')
          }
        })
      }
    })
  },
  getTotalLists(callBack) {
    mid_util.getStorage(list, {
      success: res => {
        mid_util.callBack(callBack, res)
      }
    })
  },
  getByStatus(status, callBack) {
    this.getTotalLists({
      success: res => {
        var tmp = []
        res.forEach(e => {
          if (e.status == status) {
            tmp.push(e)
          }
        })
        mid_util.callBack(callBack, tmp)
      }
    })
  },
  getToDo(callBack) {
    this.getByStatus(0, {
      success: res => {
        mid_util.callBack(callBack, res)
      }
    })
  },
  getDoing(callBack) {
    this.getByStatus(1, {
      success: res => {
        mid_util.callBack(callBack, res)
      }
    })
  },
  getDone(callBack) {
    this.getByStatus(2, {
      success: res => {
        mid_util.callBack(callBack, res)
      }
    })
  },
  addList(data, callBack) {
    mid_util.getStorage(list, {
      success: res => {
        data.list_id = res.length
        res.push(data)
        mid_util.setStorage(list, res, {
          success: res => {
            mid_util.callBack(callBack, data.list_id)
          }
        })
      }
    })
  }
}
